//
//  LoginVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/14.
//
// 【登录】登录页面

import Foundation
import UIKit

class LoginVC: TopViewController {
    let loginV = LoginView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupUI()

        reloadData()
    }

    // MARK: - Actions
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        VersionUpdateManager.shared.setupVersion()
    }

    @objc func forgotTapped() {
        navigationController?.pushViewController(ForgotVC(), animated: true)
    }

    @objc func submitTapped() {
        view.showToastActivity()
        API.login(withParam: loginV.loginM) { responseModel in
            self.view.hideToastActivity()
            if responseModel.errorCode == .Success {
                BSUser.currentUser.modifyUser(_phone: self.loginV.loginM.username, _password: self.loginV.loginM.password)
                BSUser.currentUser.modifyUser(withModal: responseModel.model?.user)
                let token = responseModel.model?.token
                let role = responseModel.model?.role
//                let user = responseModel.model?.user
                
                let isAdmin = role == UserRole.Engineer.rawValue ||
                              role == UserRole.HeadOffice.rawValue ||
                              role == UserRole.BranchOffice.rawValue ||
                              role == UserRole.SubBranchOffice.rawValue ||
                              role == UserRole.Lattice.rawValue ||
                              role == UserRole.SecurityGuard.rawValue ||
                              role == UserRole.Cleaning.rawValue
                if isAdmin {
                    UserDefaults.standard.set(token, forKey: SafetyToken)
                    UserDefaults.standard.set(role, forKey: SafetyUserRole)
                    
                    let initEbable = responseModel.model?.initEbable
                    if initEbable == true {
                        self.navigationController?.pushViewController(FinishInfoVC(), animated: true)
                    } else {
                        Utils.app.setupMain()
                        Utils.app.window?.showToast(witMsg: "登录成功")
                    }
                } else {
                    Utils.app.window?.showToast(witMsg: "暂无权限访问")
                }
            } else {
                self.view.showToast(witMsg: responseModel.errorMessage)
                self.loginV.isimgCodeHidden = false
            }
        }
    }
    
    @objc func registerTapped() {
        navigationController?.pushViewController(RegisterVC(), animated: true)
    }

    func reloadData() {
        loginV.reloadData()

        API.commonSetting { responseModel in
            if responseModel.errorCode == .Success {
                let registerEnabled = responseModel.model?.registerEnabled
                DispatchQueue.main.async {
                    self.loginV.registerBtn.isHidden = !(registerEnabled == true)
                }
            }
        }
    }

    // MARK: - Setup

    func setupUI() {
        loginV.loginBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        loginV.forgotBtn.addTarget(self, action: #selector(forgotTapped), for: .touchUpInside)
        loginV.registerBtn.addTarget(self, action: #selector(registerTapped), for: .touchUpInside)
        view.addSubview(loginV)
        loginV.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
}
